﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Drawing.Design;


namespace XnaGameServer {
    public partial class Form1 : Form {

        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            if (button1.Text == "Start") {
                Program.StartServer();
                button1.Text = "Shut down";
                textBox1.Enabled = false;
                textBox2.Enabled = false;
            }
            else {
                Program.Shutdown();
                button1.Text = "Start";
                textBox1.Enabled = true;
                textBox2.Enabled = true;
            }
        }

        private void chargerToolStripMenuItem_Click(object sender, EventArgs e) {
            Program.Load();
            propertyGrid1.SelectedObject = Program.settings;
        }

        private void sauverToolStripMenuItem_Click(object sender, EventArgs e) {
            Program.Save();
        }

        private void quitterToolStripMenuItem_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
            Program.Shutdown();
        }
    }



    // 04.02.2011 - Permet de modifier un dictionnaire facilement depuis un PropertyGrid
    class DictionaryPropertyGridAdapter : ICustomTypeDescriptor {
        IDictionary _dictionary;

        public DictionaryPropertyGridAdapter(IDictionary d) {
            _dictionary = d;
        }

        #region ICustomTypeDescriptor Membres

        public AttributeCollection GetAttributes() {
            return TypeDescriptor.GetAttributes(this, true);
        }

        public string GetClassName() {
            return TypeDescriptor.GetClassName(this, true);
        }

        public string GetComponentName() {
            return TypeDescriptor.GetComponentName(this, true);
        }

        public TypeConverter GetConverter() {
            return TypeDescriptor.GetConverter(this, true);
        }

        public EventDescriptor GetDefaultEvent() {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        public PropertyDescriptor GetDefaultProperty() {
            return null;
        }

        public object GetEditor(Type editorBaseType) {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes) {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        public EventDescriptorCollection GetEvents() {
            return TypeDescriptor.GetEvents(this, true);
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes) {
            List<DictionaryPropertyDescriptor> properties = new List<DictionaryPropertyDescriptor>();
            foreach (DictionaryEntry e in _dictionary) {
                Attribute[] propertyAttributes = null;
                if (typeof(IDictionary).IsAssignableFrom(e.Value.GetType())) {
                    propertyAttributes = new Attribute[] { new EditorAttribute(typeof(DictionaryEditor), typeof(UITypeEditor)) };
                }
                properties.Add(new DictionaryPropertyDescriptor(_dictionary, e.Key, propertyAttributes));
            }

            return new PropertyDescriptorCollection(properties.ToArray());
        }

        public PropertyDescriptorCollection GetProperties() {
            return ((ICustomTypeDescriptor)this).GetProperties(new Attribute[0]);
        }

        public object GetPropertyOwner(PropertyDescriptor pd) {
            return _dictionary;
        }

        #endregion
    }
    class DictionaryPropertyDescriptor : PropertyDescriptor {
        IDictionary _dictionary;
        object _key;

        internal DictionaryPropertyDescriptor(IDictionary d, object key, Attribute[] attribues)
            : base(key.ToString(), attribues) {
            _dictionary = d;
            _key = key;
        }

        public override Type PropertyType {
            get {
                if (_dictionary[_key] != null)
                    return _dictionary[_key].GetType();
                else
                    return typeof(object);
            }
        }

        public override void SetValue(object component, object value) {
            _dictionary[_key] = value;
        }
        public override object GetValue(object component) {
            return _dictionary[_key];
        }
        public override bool IsReadOnly {
            get { return false; }
        }
        public override Type ComponentType {
            get { return null; }
        }
        public override bool CanResetValue(object component) {
            return false;
        }
        public override void ResetValue(object component) {
        }
        public override bool ShouldSerializeValue(object component) {
            return false;
        }
    }
    class DictionaryEditor : UITypeEditor {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) {
            // gives us an ellipsis, rather than a drop-down arrow. Otherwise, it's pretty much cosmetic.
            return UITypeEditorEditStyle.Modal;
        }
        //public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {
        //    if (value is IDictionary) {
        //        InspectorForm form = new InspectorForm();
        //        form.SelectedObject = (IDictionary)value;
        //        form.ShowDialog();
        //    }
        //    return value;
        //}
    }

}
